/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

/**
 * Check for update of predefined rules.
 *
 * @author Alexander Kozlov
 */
var wsshRulesImportManager = {
    logger: yrprxFactory.createLogger("wsshRulesImportManager")
};

wsshRulesImportManager.loadProfileFromURLAsync = function(url, callback, ifModifiedSince)
{
    try
    {
        if (!url || !callback)
            throw "Invalid parameters";

        var manager = this;

        var req = wsshWindowManager.newXMLHttpRequest();
        req.open('GET', url, true);

        if (ifModifiedSince && ifModifiedSince.length > 0)
            req.setRequestHeader("If-Modified-Since", ifModifiedSince);

        req.onreadystatechange = function (evt)
        {
            if (req.readyState != 4)
                return;

            try
            {
                if (req.status == 304) // Not Modified
                {
                    manager.logger.debug("loadProfileFromURLAsync: Resource is Not Modified.");
                    callback(null, ifModifiedSince);
                }
                else if (req.status == 200)
                {
                    manager.logger.debug("loadProfileFromURLAsync: Resource is ready.");
                    callback(wsshWebProfileSerializer.load(req.responseXML), req.getResponseHeader("Last-Modified"));
                }
                else
                {
                    if (manager.logger.enabled) manager.logger.error("loadProfileFromURLAsync: Error loading page. Status: " + req.status);
                    callback(null, null);
                }
            }
            catch(ex)
            {
                if (manager.logger.enabled) manager.logger.error("loadProfileFromURLAsync: Exception in onreadystatechange: " + ex);
            }
        };
        req.send(null);

        return true;
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("loadProfileFromURLAsync: Exception: " + ex);

        return false;
    }
}

wsshRulesImportManager.containsJavaScript = function(items)
{
    for (var i in items)
    {
        for (var j in items[i].pages)
        {
            var page = items[i].pages[j];
            if (page.isJavaScriptTemplate())
                return true;
        }
    }
    return false;
}

wsshRulesImportManager.disableJavaScript = function(items)
{
    this.usingJavaScript(items, false);
}

wsshRulesImportManager.enableJavaScript = function(items)
{
    this.usingJavaScript(items, true);
}

wsshRulesImportManager.usingJavaScript = function(items, enable)
{
    for (var i in items)
    {
        for (var j in items[i].pages)
        {
            var page = items[i].pages[j];
            if (page.isJavaScriptTemplate())
                page.disabled = !enable;
        }
    }
}

wsshRulesImportManager.countRules = function(items)
{
    var count = 0;
    try
    {
        for (var i in items)
        {
            count += items[i].pages.length;
        }
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("countRules: Exception: " + ex);
    }
    return count;
}
